package cn.lijiajia3515.cairo.auth.controller;

import cn.lijiajia3515.cairo.core.business.DefaultBusiness;
import cn.lijiajia3515.cairo.core.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Slf4j
@Controller
@RequestMapping
public class RouteController {
	private final HttpServletRequest request;
	private final HttpSession session;

	public RouteController(HttpServletRequest request, HttpSession session) {
		this.request = request;
		this.session = session;
	}

	@GetMapping(value = {"", "index", "index.html"})
	@PermitAll
	public String index() {
		return "index";
	}

	@GetMapping("login")
	public String login() {
		return "login";
	}

	@GetMapping("logout")
	public String logout() {
		return "logout";
	}

	@GetMapping("profile")
	@PreAuthorize("isAuthenticated()")
	String profile() {
		return "profile";
	}

	@GetMapping(value = "/ex")
	@PermitAll
	public String ex(@AuthenticationPrincipal Object principal) {
		throw new BusinessException("测试错误", DefaultBusiness.Failed);
	}
}
